/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.datav.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.database.datalink.entity.DataSource;
import com.jeeplus.modules.database.datalink.jdbc.DBPool;
import com.jeeplus.modules.database.datalink.service.DataSourceService;
import com.jeeplus.modules.datav.entity.DataScreen;
import com.jeeplus.modules.datav.service.DataScreenService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 大屏Controller
 * @author 刘高峰
 * @version 2021-03-29
 */
@RestController
@RequestMapping(value = "/datav/dataScreen")
public class DataScreenController extends BaseController {

	@Autowired
	private DataScreenService dataScreenService;
	@Autowired
	private DataSourceService dataSourceService;

	@ModelAttribute
	public DataScreen get(@RequestParam(required=false) String id) {
		DataScreen entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = dataScreenService.get(id);
		}
		if (entity == null){
			entity = new DataScreen();
		}
		return entity;
	}

	/**
	 * 大屏设计列表数据
	 */
	@RequiresPermissions("datav:dataScreen:list")
	@GetMapping("list")
	public AjaxJson list(DataScreen dataScreen, HttpServletRequest request, HttpServletResponse response) {
		Page<DataScreen> page = dataScreenService.findPage(new Page<DataScreen>(request, response), dataScreen);
		return AjaxJson.success().put("page",page);
	}

	/**
	 * 根据Id获取大屏设计数据
	 */
	@RequiresPermissions(value={"datav:dataScreen:view","datav:dataScreen:add","datav:dataScreen:edit"},logical=Logical.OR)
	@GetMapping("queryById")
	public AjaxJson queryById(DataScreen dataScreen) {
		return AjaxJson.success().put("dataScreen", dataScreen);
	}

	/**
	 * 复制大屏
	 */
	@GetMapping("copy")
	public AjaxJson copy(DataScreen dataScreen) {
		dataScreen.setId (null);
		dataScreen.setName (dataScreen.getName () + "-copy");
		dataScreenService.save (dataScreen);
		return AjaxJson.success("复制大屏成功!");
	}


	/**
	 * 保存大屏设计
	 */
	@RequiresPermissions(value={"datav:dataScreen:add","datav:dataScreen:edit"},logical=Logical.OR)
	@PostMapping("save")
	public AjaxJson save(DataScreen dataScreen) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(dataScreen);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		dataScreenService.save(dataScreen);//保存
		return AjaxJson.success("保存大屏设计成功");
	}


	/**
	 * 批量删除大屏设计
	 */
	@RequiresPermissions("datav:dataScreen:del")
	@DeleteMapping("delete")
	public AjaxJson delete(String ids) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			dataScreenService.delete(new DataScreen(id));
		}
		return AjaxJson.success("删除大屏设计成功");
	}


	@PostMapping(value = "/sqlQuery")
	public AjaxJson exec(@RequestParam(value = "db", required = false)String db, @RequestParam(value = "sql", required = false)String sql, @RequestParam(value = "field[]", required = false)String[] field, @RequestParam(value = "defaultValue[]", required = false)String[] defaultValue) {
		AjaxJson j = new AjaxJson();
		DataSource dataSource = dataSourceService.get(db);
		if (dataSource == null) {
			j.setSuccess(false);
			j.setMsg("数据库链接不存在!");
			return j;
		}

		JdbcTemplate jdbcTemplate = DBPool.getInstance().getDataSource(dataSource.getEnName());
		if (sql.contains("delete") || sql.contains("update")) {
			j.setSuccess(false);
			j.setMsg("只允许查询操作!");
			return j;
		};

		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		j.put ("data", list);

		return j;
	}

}
